package heuristic;
import static skeleton.Board.*;

public class AdjStyleCounter {
	public int[] mine, ops;
	int incx[] = {0, 1, 1, 1, 0, -1, -1, -1};
	int incy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
	

	private boolean outBound(int chessx, int chessy) {
		if (chessx<LEN && chessx>=0 && chessy<LEN && chessy >=0) return false;
		return true;
	}

	public void count(int[][] data, int myColor, int opColor, int x, int y) {
		mine = new int[6];
		ops = new int[6];
		int[] chessNum = new int[8];
		int[] len = new int[8];
		for (int i=0; i<8; ++i) {
			int empty = 0;
			int a, b, p = 1;
			while (true) {
				a = x + p * incx[i];
				b = y + p * incy[i];
				if (!outBound(a, b)) {
					if (data[a][b] == myColor) {
						++chessNum[i];
						empty = 0;
					}
					else if (data[a][b] == EMPTY) {
						++empty;
						if (empty == 2) {
							++len[i];
							for (int j=1; j<3; ++j) {
								a = x + (p + j) * incx[i];
								b = y + (p + j) * incy[i];
								if (!outBound(a, b) && data[a][b] != opColor)
									++len[i];
							}
							break;
						}
					}
					else break;
				}
				else break;
				++p;
				++len[i];
			}
		}
		int t;
		for (int i=0; i<4; ++i) {
			if (len[i] + len[i+4] < 4) continue;
			t = chessNum[i] + chessNum[i+4];
			if (t >= 5) ++mine[5];
			else ++mine[t];
		}
		for (int i=0; i<8; ++i) {
			int empty = 0;
			int a, b, p = 1;
			chessNum[i] = 0;
			len[i] = 0;
			while (true) {
				a = x + p * incx[i];
				b = y + p * incy[i];
				if (!outBound(a, b)) {
					if (data[a][b] == opColor) {
						++chessNum[i];
						empty = 0;
					}
					else if (data[a][b] == EMPTY) {
						++empty;
						if (empty == 2) {
							++len[i];
							for (int j=1; j<3; ++j) {
								a = x + (p + j) * incx[i];
								b = y + (p + j) * incy[i];
								if (!outBound(a, b) && data[a][b] != opColor)
									++len[i];
							}
							break;
						}
					}
					else break;
				}
				else break;
				++p;
				++len[i];
			}
		}
		for (int i=0; i<4; ++i) {
			if (len[i] + len[i+4] < 4) continue;
			t = chessNum[i] + chessNum[i+4];
			if (t >= 5) ++ops[5];
			else ++ops[t];
		}
	}
}
